package com.zzsy.carosshopprojects.activitys.home.mainpage.incrementservice;

import android.content.Context;
import android.view.View;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.adapter.IncrementServiceAdapter;
import com.zzsy.carosshopprojects.base.BaseFragment;
import com.zzsy.carosshopprojects.bean.CarCardBean;
import com.zzsy.carosshopprojects.bean.ServiceCardBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.sunmiprint.PrintHelper;

import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * 增值服务
 */
public class IncrementServiceFragment extends BaseFragment {
    public static IncrementServiceFragment newInstance() {
        IncrementServiceFragment incrementServiceFragment = new IncrementServiceFragment();
        return incrementServiceFragment;
    }

    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    private IncrementServiceAdapter incrementServiceAdapter;

    @Override
    protected int initLayout() {
        return R.layout.fg_incrementservice;
    }

    @Override
    protected void initView(View view) {
        initTitle(view, getResources().getString(R.string.IncrementService), getActivity());
        incrementServiceAdapter = new IncrementServiceAdapter(getContext(), null, 0);
        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(incrementServiceAdapter);
        incrementServiceAdapter.setOnItemClickListener((v, position) -> {
            printDialog(incrementServiceAdapter.getListData().get(position).getId(), incrementServiceAdapter.getListData().get(position).getCardName());
        });
    }

    @Override
    protected void initData(Context mContext) {

    }

    @Override
    protected void loadMethod() {
        getData();
    }

    /**
     * 获取增值服务数据
     */
    private void getData() {
        RequestUtils.ServiceCardList(getContext(), DataUtil.getInstance().getStoreId(), new MyObserver<List<ServiceCardBean>>(getContext()) {
            @Override
            public void onSuccess(List<ServiceCardBean> result) {
                incrementServiceAdapter.setListData(result);
                incrementServiceAdapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
            }
        });
    }

    /**
     * 创建小票
     */
    private void createCarP(String serviceId, String cardName) {
        RequestUtils.CreateWashCarCard(getContext(), DataUtil.getInstance().getCompanyId(), serviceId, DataUtil.getInstance().getStoreId(), DataUtil.getInstance().getTerminalId(), new MyObserver<CarCardBean>(getContext()) {
            @Override
            public void onSuccess(CarCardBean result) {
                PrintHelper.getInstance().printBarCodeContent(result, cardName);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(getContext(), errorMsg);
            }
        });
    }

    public void printDialog(String serviceId, String cardName) {
//        final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
//        dialog.setTitle(getContext().getResources().getString(R.string.tips))
//                .setMessage(getContext().getResources().getString(R.string.surePrintTicket))
//                .setPositiveButton(getContext().getResources().getString(R.string.sure), (dialog1, which) -> {
//                    createCarP(serviceId, cardName);
//                })
//                .setNegativeButton(getContext().getResources().getString(R.string.cancel), (dialog12, which) -> {
//                });
//        AlertDialog dialog_dialog = dialog.create();
//        dialog_dialog.show();
        createCarP(serviceId, cardName);
    }
}
